﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class ChiTietDealDAO
    {
        private FDeal4Entities db;

        public ChiTietDealDAO()
        {
            db = new FDeal4Entities();
        }

        public List<CHITIETDEAL> LietKe()
        {
            return db.CHITIETDEAL.ToList();
        }

        public CHITIETDEAL LayID(string id)
        {
            var tl = db.CHITIETDEAL.SingleOrDefault(t => t.IDDEAL == id);
            return tl;
        }

        public CHITIETDEAL TimTen(String keyWord)
        {
            var tl = db.CHITIETDEAL.SingleOrDefault(t => t.TIEUDE.Contains(keyWord));
            return tl;
        }

        public bool Them(CHITIETDEAL tl)
        {
            try
            {
                db.CHITIETDEAL.AddObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Xoa(CHITIETDEAL tl)
        {
            try
            {
                db.CHITIETDEAL.DeleteObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Sua(CHITIETDEAL tl)
        {
            try
            {
                CHITIETDEAL t1 = db.CHITIETDEAL.Single(t => t.IDDEAL == tl.IDDEAL);
                t1.IDDEAL = tl.IDDEAL;
                t1.TIEUDE = tl.TIEUDE;
                t1.LOAIDEAL = tl.LOAIDEAL;
                t1.TOMTATDEAL = tl.TOMTATDEAL;
                t1.HINHANH = tl.HINHANH;
                t1.GIAGOC = tl.GIAGOC;
                t1.DONVI = tl.DONVI;
                t1.GIAM = tl.GIAM;
                t1.GIACONLAI = tl.GIACONLAI;
                t1.SONGUOIMUA = tl.SONGUOIMUA;
                t1.THOIGIANCONLAI = tl.THOIGIANCONLAI;
                t1.SODEALBAN = tl.SODEALBAN;
                t1.DIEUKIEN = tl.DIEUKIEN;
                t1.DIEMNOIBAT = tl.DIEMNOIBAT;
                t1.DIACHI = tl.DIACHI;
                t1.BANDO = tl.BANDO;
                t1.LUOTXEM = tl.LUOTXEM;
                t1.NGAYDANG = tl.NGAYDANG;
                t1.NGUOIDANG = tl.NGUOIDANG;
                t1.DIADIEMBANDEAL = tl.DIADIEMBANDEAL;                
            

                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
            	return false;
            }
        }

        public List<CHITIETDEAL> Lay4ChiTietBatKi()
        {            
            return db.CHITIETDEAL.Take(4).ToList();
        }

        public List<CHITIETDEAL> Lay4theodanhmuc(string iddanhmuc)
        {
            List<CHITIETDEAL> dsChiTietDeal = db.CHITIETDEAL.ToList();
            for (int i = 0; i < dsChiTietDeal.Count; i++)
            {
                if (dsChiTietDeal[i].LOAIDEAL.ToString() != iddanhmuc)
                {
                    dsChiTietDeal.Remove(dsChiTietDeal[i]);
                }
            }

            return dsChiTietDeal.Take(4).ToList();
        }
    }
}
